package com.demo.mybaits.db;

import javax.sql.DataSource;

import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.demo.mybaits.utils.StringUtil;

class MysqlDataSource extends DataSourceStrategy {

	private final static Logger log = LoggerFactory.getLogger(MysqlDataSource.class);
	
	@Override
	public DataSource getDataSource() {
		String username = "root";
		String password = "123456";
		String ip = "134.175.31.189";
		String port = "3306";
		String schema = "demo";
		String driverName = "com.mysql.jdbc.Driver";
		Integer max_active = 10000;
		Integer initial_size = 5;
		Integer max_wait = 2000;
		Integer remove_abandoned_timeout = 60;
		Integer min_idle = 100;

		PoolProperties p = new PoolProperties();
		p.setUrl(StringUtil.getString("jdbc:mysql://{}:{}/{}?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC", ip, port,
				schema));
		p.setDriverClassName(driverName);
		p.setUsername(username);
		p.setPassword(password);

		p.setJmxEnabled(true);
		p.setTestWhileIdle(false);
		p.setTestOnBorrow(true);
		p.setValidationQuery("SELECT 1 FROM DUAL");
		p.setTestOnReturn(false);
		p.setValidationInterval(30000);
		p.setTimeBetweenEvictionRunsMillis(30000);
		p.setMaxActive(max_active);
		p.setInitialSize(initial_size);
		p.setMaxWait(max_wait);
		p.setRemoveAbandonedTimeout(remove_abandoned_timeout);
		p.setMinEvictableIdleTimeMillis(30000);
		p.setMinIdle(min_idle);
		p.setLogAbandoned(true);
		p.setRemoveAbandoned(true);
		p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
				+ "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");

		org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
		dataSource.setPoolProperties(p);
		
		log.info("datasource初始化完成");
		return dataSource;
	}

}
